﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace CinemabookingWCFApp.BookedSeats
{
    public class BookedSeatsService : IBookedSeatsService
    {
        #region
        CinemabookingDBDataContext db = new CinemabookingDBDataContext();
        #endregion

        public List<BookedSeat> GetAllBookedSeats()
        {
            var queryResult = (from x in db.BookedSeats
                               select x);
            return queryResult.ToList();
        }

        public List<BookedSeat> GetBookedSeat(int BookedId)
        {
            var queryResult = (from x in db.BookedSeats
                               where x.id == BookedId
                               select x);
            return queryResult.ToList();
        }

        public List<BookedSeat> GetAllBookedSeatsOnMovieRuntime(int RuntimeId)
        {
            var queryResult = (from x in db.BookedSeats
                               where x.runtimeInfo == RuntimeId
                               select x);
            return queryResult.ToList();
        }
        
        public List<BookedSeat> GetAllBookedSeatsToABooking(string BookId)
        {
            var queryResult = (from x in db.BookedSeats
                               where x.bookingInfo == BookId
                               select x);
            return queryResult.ToList();
        }

        public void SaveBookedSeat(int Seat, string BookInfo, int Runtime)
        {
            BookedSeat newBooked = new BookedSeat { seatInfo = Seat, bookingInfo = BookInfo, runtimeInfo = Runtime };

            newBooked.seatInfo = Seat;
            newBooked.bookingInfo = BookInfo;
            newBooked.runtimeInfo = Runtime;

            db.BookedSeats.InsertOnSubmit(newBooked);
            db.SubmitChanges();
        }

        public void DeleteBookedSeat(int BookedId)
        {
            BookedSeat delBooked = (from x in db.BookedSeats
                                    where x.id == BookedId
                                    select x).FirstOrDefault();
            if (delBooked !=null)
            {
                db.BookedSeats.DeleteOnSubmit(delBooked);
                db.SubmitChanges();
            }
        }

        public void DeleteBookedSeatsOnPhoneNr(string BookedPhonenr)
        {
            List<BookedSeat> lst = new List<BookedSeat>();
                
            var queryResult = (from x in db.BookedSeats
                                   where x.bookingInfo == BookedPhonenr                                   
                                   select x);
                        
            lst.AddRange(queryResult);
                        
            db.BookedSeats.DeleteAllOnSubmit(lst);
            db.SubmitChanges();
            
        }

        public void UpdateBookedSeat(int BookedId, int Seat, string BookInfo, int Runtime)
        {
            BookedSeat updBooked = (from x in db.BookedSeats
                                    where x.id == BookedId
                                    select x).Single();

            updBooked.seatInfo = Seat;
            updBooked.bookingInfo = BookInfo;
            updBooked.runtimeInfo = Runtime;

            db.SubmitChanges();
        }

        
    }
}